//package com.lplb.coreconfig.config;
//
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.time.LocalDateTime;
//import java.time.ZoneId;
//
///**
// * 用户操作日志
// *
// * @author wbg
// * @date 2019-11-23 9:49
// */
//@Slf4j
//@Aspect
//@Component
//public class OperationLogAspect {
////
////    @Autowired
////    private MongoTemplate mongoTemplate;
//
//    @Around("@annotation(org.springframework.web.bind.annotation.PostMapping)")
//    public Object aroundSave(ProceedingJoinPoint proceed) {
//        return saveOpLogs(proceed);
//    }
//
//    @Around("@annotation(org.springframework.web.bind.annotation.PutMapping)")
//    public Object aroundUpdate(ProceedingJoinPoint proceed) {
//        return saveOpLogs(proceed);
//    }
//
//    @Around("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
//    public Object aroundDelete(ProceedingJoinPoint proceed) {
//        return saveOpLogs(proceed);
//    }
//
//    private Object saveOpLogs(ProceedingJoinPoint proceed) {
//
//        Object res = null;
//        //获取请求参数
//        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//        HttpServletRequest request = sra.getRequest();
//        //获取用户真实ip
//        String ip = getIpAddress(request);
//        //返回请求行中的资源名称
//        String uri = request.getRequestURI();
//        //获得客户端发送请求的完整url
//        String url = request.getRequestURL().toString();
//        //返回发出请求的客户机的主机名
//        String host = request.getRemoteHost();
//        //返回发出请求的客户机的端口号
//        int port = request.getRemotePort();
//        log.info("用户当前的ip地址为：{}，使用的设备名称：{}，发出请求的设备端口：{}，请求的资源名：{}，完整的url：{}", ip, host, port, uri, url);
//        Object[] args = proceed.getArgs();
//        MethodSignature signature = (MethodSignature) proceed.getSignature();
//        ApiOperation annotation = signature.getMethod().getAnnotation(ApiOperation.class);
//        String className = proceed.getTarget().getClass().getName();
//        String packageName = proceed.getTarget().getClass().getPackage().getName();
//        String nickname = annotation.nickname();
//        String operationName = annotation.value();
//        log.info("服务器分配的Servlet名：{}，存在包：{}，模块名称：{}，操作名称：{}", className, packageName, nickname, annotation.value());
//        //  LogOperation operationLog = new LogOperation();
//        Long start = LocalDateTime.now().atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();
//        try {
//            res = proceed.proceed();
//            //   operationLog.setResponse(res.toString());
//        } catch (Throwable e) {
//            //  operationLog.setException(e.getMessage());
//        }
//        LocalDateTime now = LocalDateTime.now();
//        Long end = now.atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();
//
////        operationLog.setIpAddr(ip);
////        operationLog.setUrl(url);
////        operationLog.setMethod(annotation.httpMethod());
////        operationLog.setRequestArgs(JSON.toJSONString(args));
////        operationLog.setAccount(request.getParameter(Constant.Param.CURRENT_USER_ACCOUNT));
////        operationLog.setUserName(request.getParameter(Constant.Param.CURRENT_USER_NAME));
////        operationLog.setTakeTime(end - start);
////        operationLog.setModel(nickname);
////        operationLog.setOperation(operationName);
////        operationLog.setOperateTime(now);
//        try {
//            //String phone = (String) JSONObject.parseObject(JSON.toJSONString(args[0])).get("phone");
//            //operationLog.setPhone(phone == null ? null : phone);
//        } catch (Exception e) {
//
//        } finally {
//            //  mongoTemplate.insert(operationLog);
//            return res;
//        }
//    }
//
//    private static String getIpAddress(HttpServletRequest request) {
//        String unknown = "unknown";
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_CLIENT_IP");
//        }
//        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//        }
//        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//        return ip;
//    }
//}
//
